package com.ai.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.CharsetUtil;
import com.ai.entity.Call;
import com.ai.exception.AiException;

import java.io.File;
import java.io.IOException;
import java.util.List;

public class ReadOrWriteFileUtil {
    public static final String SQL_FILE_PATH = "D:\\AI-Deal\\ALL.csv";

    public static final String BASE_PATH = "D:\\AI-Deal";

    public static void readCsv() {
        CsvReader csvReader = CsvUtil.getReader();
        List<Call> allCsv = csvReader.read(ResourceUtil.getReader(SQL_FILE_PATH, CharsetUtil.CHARSET_GBK), Call.class);

    }

    public static synchronized void syncWriteTxt(String dirName, String fileName, String content) {
        FileWriter fileWriter = new FileWriter(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        fileWriter.append(content + "\n");
    }

    public static void writeTxt(String dirName, String fileName, String content) {
        FileWriter fileWriter = new FileWriter(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        fileWriter.append(content + "\n");
    }

    public static void coverWriteTxt(String dirName, String fileName, String content) {
        FileWriter fileWriter = new FileWriter(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        fileWriter.write(content + "\n");
    }

    public static synchronized void writeCoverTxt(String dirName, String fileName, String content) {
        FileWriter fileWriter = new FileWriter(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        fileWriter.write(content + "\n");
    }


    public static List<String> readTxtLines(String dirName, String fileName) {
        FileReader fileReader = new FileReader(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        return fileReader.readLines();
    }

    public static String readTxt(String dirName, String fileName) {
        FileReader fileReader = new FileReader(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        return fileReader.readString();
    }

    public static long getFileSize(String dirName, String fileName) {
        return FileUtil.size(new File(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt")) / 1024;

    }

    public static Boolean deleteFile(String dirName, String fileName) {
        File file = new File(BASE_PATH + "\\" + dirName + "\\" + fileName + ".txt");
        return file.delete();
    }

    public static void deleteLikeFile(String dirName, String fileName) {
        File directory = new File(BASE_PATH + "\\" + dirName + "\\");
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isFile() && file.getName().contains(fileName)) {
                        boolean isDeleted = file.delete();
                        if (isDeleted) {
                            System.out.println("成功删除文件：" + file.getName());
                        } else {
                            System.out.println("无法删除文件：" + file.getName());
                        }
                    }
                }
            }
        } else {
            System.out.println("给定的路径不是一个文件夹");
        }
    }

    public static void main(String[] args) {
        deleteLikeFile("C1","常规产后服务项目");
    }

}
